
#
# Copyright © 2005-2010 Instigate CJSC, Armenia
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

export pwd := $(shell pwd)

ifndef pwd
$(error pwd is not defined)
endif

ifndef test_type
$(error test-type is not defined)
endif

ifndef test_subset
$(error test_subset is not defined)
endif

ifndef acceptance_tests_dir
$(error acceptance_tests_dir is not defined)
endif

ifndef black_box_tests_dir
$(error black_box_tests_dir is not defined)
endif

ifndef regression_tests_dir
$(error regression_tests_dir is not defined)
endif

ifndef acceptance_tests_results_dir
$(error acceptance_tests_results_dir is not defined)
endif

ifndef black_box_tests_results_dir
$(error black_box_tests_results_dir is not defined)
endif

ifndef regression_tests_results_dir
$(error regression_tests_results_dir is not defined)
endif

ifndef test_result
$(error test_result is not defined)
endif

#ifndef coverage_statistics
#$(error coverage_statistics is not defined)
#endif

acceptance_sub_dirs := $(wildcard $(acceptance_tests_dir)/*)
acceptance_sub_results_dirs := $(subst $(acceptance_tests_dir),\
		$(acceptance_tests_results_dir),$(acceptance_sub_dirs))
black_box_sub_dirs := $(wildcard $(black_box_tests_dir)/*)
black_box_sub_results_dirs := $(subst $(black_box_tests_dir),\
		$(black_box_tests_results_dir),$(black_box_sub_dirs))
regression_sub_dirs := $(wildcard $(regression_tests_dir)/*)
regression_sub_results_dirs := $(subst $(regression_tests_dir),\
		$(regression_tests_results_dir),$(regression_sub_dirs))

ifeq ($(test_type),acceptance_tests)
test_sub_dirs := $(acceptance_sub_dirs)
test_sub_results_dirs := $(acceptance_sub_results_dirs)
else
ifeq ($(test_type),black_box_tests)
test_sub_dirs := $(black_box_sub_dirs)
test_sub_results_dirs := $(black_box_sub_results_dirs)
else
ifeq ($(test_type),regression_tests)
test_sub_dirs := $(regression_sub_dirs)
test_sub_results_dirs := $(regression_sub_results_dirs)
else
ifeq ($(test_type),all)
test_sub_dirs := $(acceptance_sub_dirs) $(black_box_sub_dirs) \
					$(regression_sub_dirs)
test_sub_results_dirs := $(acceptance_sub_results_dirs) \
					$(black_box_sub_results_dirs) \
					$(regression_sub_results_dirs)
endif
endif
endif
endif

sub_tests_results := $(addsuffix /result.txt, $(test_sub_results_dirs))

.PHONY: test
test: $(preliminary_actions) create_test_directories $(test_result)

.PHONY: create_test_directories
create_test_directories: 
ifneq ($(words $(test_sub_results_dirs)),0)
	@mkdir -p -m 755 $(test_sub_results_dirs)
endif

get_number_of = $(shell grep -r $(1) $(shell ls $(sub_tests_results) 2> \
		/dev/null) | wc -l)

# TODO: The way number of total run tests is calculated in general is not
# correct because we may define only subset of tests to be run and thus the
# total number will be decreased and will not be equal to the number of TCL
# files in $(test_sub_dirs)
$(test_result): $(test_sub_dirs)
ifneq ($(words $(test_sub_results_dirs)),0)
	@echo "" > $@
	@echo " TOP LEVEL TEST RESULTS" >> $@
	@echo "" >> $@ 
	@echo " PROGRAM:" >> $@
	@echo "         Path - $(PWD)" >> $@
ifdef program_name
ifdef program_path
	@echo "         Version - "`$(program_path)/$(program_name) --version` >> $@
else
	@echo "         Version - "`./$(program_name) --version` >> $@
endif
endif
ifneq (`svn info 2> /dev/null`,)
	@echo "         Repository version - "`svn info 2> /dev/null | grep Revision | sed 's/Revision:\ //g'` >> $@
endif
	@echo "" >> $@ 
	@echo " PLATFORM:" >> $@
	@echo "         Hostname - "`uname -n` >> $@
	@echo "         Architecture - "`uname -m` >> $@
	@echo "         OS - "`cat /etc/issue` >> $@
	@echo "         Compiler - "`gcc --version | grep "gcc" -i` >> $@
	@echo "         TCL version - 8.4" >> $@
	@echo "         make version - "`make --version | grep "Make" -i` >> $@
	@echo "" >> $@
	@echo " SUMMARY:" >> $@
	@echo "         Total -" `expr $(call get_number_of, PASS) + \
	$(call get_number_of, FAIL) + $(call get_number_of, "Expected Fail") + \
	$(call get_number_of, "Unexpected Pass") + \
	$(call get_number_of, SKIP) + $(call get_number_of, CRASH)` >> $@
	@echo "         Passed -" $(call get_number_of, PASS) >> $@
	@echo "         Failed -" $(call get_number_of, FAIL) >> $@
	@echo "         Expected fails -" $(call get_number_of, \
		"Expected Fail") >> $@
	@echo "         Unexpected passes -" $(call get_number_of,  \
		"Unexpected Pass") >> $@
	@echo "         Skipped -" $(call get_number_of, SKIP) >> $@
	@echo "         Crash -" $(call get_number_of, CRASH) >> $@
	@echo "" >> $@
	@echo " LEGEND:" >> $@
	@echo "       Total - total number of all test-cases examined." >> $@
	@echo "" >> $@
	@echo "       Passed - number of test-cases which were executed \
	and succeeded." >> $@
	@echo "" >> $@
	@echo "       Failed - number of test-cases which were executed \
	but failed." >> $@
	@echo "" >> $@
	@echo "       Expected fails - number of test-cases which were \
	executed and failed," >> $@
	@echo "               but are expected to fail due to known bugs." >> $@
	@echo "" >> $@
	@echo "       Unexpected pass - number of test-cases \
	which were executed and succeeded," >> $@ 
	@echo "               although was expected to fail \
	due to known bug." >> $@
	@echo "" >> $@
	@echo "       Skipped - number of test-cases which are marked \
	by developer to be" >> $@
	@echo "               skipped for some reason \
	(e.g. to distinguish from bugs/fails)."  >> $@
	@echo "               These test-cases are not executed." >> $@
	@echo "" >> $@
	@echo "       Crashed - number of test-cases which were executed," >> $@
	@echo "               but execution was interrupted \
	due to a tool failure" >> $@
	@echo "               (e.g. coredump) or some other external reason " \
		>> $@
	@echo "               (e.g.  environment problem, disk full, etc.)." \
		>> $@
	@echo "" >> $@
	@echo "RESULTS BY TEST SERIES:" >> $@
	@echo "" >> $@
	@for i in `ls $(sub_tests_results) 2> /dev/null`; do \
		echo -n $$i | sed 's/\/result.txt//' | sed 's/.*\///' >> $@; \
		echo " - " `cat $$i`>> $@; \
		echo "" >> $@; \
		echo "---------------------------------------" >> $@; \
		echo "" >> $@; \
	done
endif
#ifeq ($(coverage), enabled)
#	@rm $(coverage_statistics) -rf
#	@for i in $(coverage_report); do \
#		echo --------------- $$i --------------- | sed 's/obj\///' | \
#			tr '[a-z]' '[A-Z]' >> $(coverage_statistics); \
#		echo "" >> $(coverage_statistics); \
#		cd $$i; \
#		l=`ls *.gcno`; \
#		for j in $$l; do \
#			$(gcov) -b -n $$j | \
#				grep `basename $$j gcno`cpp -A 4 >> \
#				../../$(coverage_statistics); \
#			echo "" >> ../../$(coverage_statistics); \
#		done;\
#		cd ../..; \
#	done
#endif


.PHONY: $(test_sub_dirs)
$(test_sub_dirs):
	@echo "Entering $@"
	@$(MAKE) --no-print-directory -C $@
	@echo "Leaving $@"
